// n.d(e, { r: () => MeshBVH });
import * as THREE from "three"
class MeshBVHNode {
  boundingData: any
  constructor() {}
}
function i(t, e, n) {
  return (n.min.x = e[t]), (n.min.y = e[t + 1]), (n.min.z = e[t + 2]), (n.max.x = e[t + 3]), (n.max.y = e[t + 4]), (n.max.z = e[t + 5]), n
}
function s(t) {
  let e = -1,
    n = -1 / 0
  for (let r = 0; r < 3; r++) {
    const i = t[r + 3] - t[r]
    i > n && ((n = i), (e = r))
  }
  return e
}
function o(t, e) {
  e.set(t)
}
function a(t, e, n) {
  let r, i
  for (let s = 0; s < 3; s++) {
    const o = s + 3
    ;(r = t[s]), (i = e[s]), (n[s] = r < i ? r : i), (r = t[o]), (i = e[o]), (n[o] = r > i ? r : i)
  }
}
function c(t) {
  const e = t[3] - t[0],
    n = t[4] - t[1],
    r = t[5] - t[2]
  return 2 * (e * n + n * r + r * e)
}
const u = Math.pow(2, -24)
function l(t, e, n, r, i = null) {
  let s = 1 / 0,
    o = 1 / 0,
    a = 1 / 0,
    c = -1 / 0,
    u = -1 / 0,
    l = -1 / 0,
    f = 1 / 0,
    h = 1 / 0,
    d = 1 / 0,
    p = -1 / 0,
    w = -1 / 0,
    g = -1 / 0
  const y = null !== i
  for (let r = 6 * e, i = 6 * (e + n); r < i; r += 6) {
    const e = t[r + 0],
      n = t[r + 1],
      i = e - n,
      x = e + n
    i < s && (s = i), x > c && (c = x), y && e < f && (f = e), y && e > p && (p = e)
    const m = t[r + 2],
      B = t[r + 3],
      b = m - B,
      V = m + B
    b < o && (o = b), V > u && (u = V), y && m < h && (h = m), y && m > w && (w = m)
    const T = t[r + 4],
      M = t[r + 5],
      P = T - M,
      S = T + M
    P < a && (a = P), S > l && (l = S), y && T < d && (d = T), y && T > g && (g = T)
  }
  ;(r[0] = s), (r[1] = o), (r[2] = a), (r[3] = c), (r[4] = u), (r[5] = l), y && ((i[0] = f), (i[1] = h), (i[2] = d), (i[3] = p), (i[4] = w), (i[5] = g))
}
const f = 32,
  h = new Array(f).fill(null).map(() => ({
    count: 0,
    bounds: new Float32Array(6),
    rightCacheBounds: new Float32Array(6),
    candidate: 0
  })),
  d = new Float32Array(6)
function p(t, e) {
  function n(e, r, i, u = null, V = 0) {
    if (
      (!b &&
        V >= y &&
        ((b = !0), x && (console.warn(`MeshBVH: Max depth of ${y} reached when generating BVH. Consider increasing maxDepth.`), console.warn(t))),
      i <= m || V >= y)
    )
      return (e.offset = r), (e.count = i), e
    const T = (function (t, e, n, r, i, u) {
      let l = -1,
        p = 0
      if (0 === u) (l = s(e)), -1 !== l && (p = (e[l] + e[l + 3]) / 2)
      else if (1 === u)
        (l = s(t)),
          -1 !== l &&
            (p = (function (t, e, n, r) {
              let i = 0
              for (let s = e, o = e + n; s < o; s++) i += t[6 * s + 2 * r]
              return i / n
            })(n, r, i, l))
      else if (2 === u) {
        const s = c(t)
        let u = 1.25 * i
        const w = 6 * r,
          g = 6 * (r + i)
        for (let t = 0; t < 3; t++) {
          const r = e[t],
            y = (e[t + 3] - r) / f
          for (let t = 0; t < f; t++) {
            const e = h[t]
            ;(e.count = 0), (e.candidate = r + y + t * y)
            const n = e.bounds
            for (let t = 0; t < 3; t++) (n[t] = 1 / 0), (n[t + 3] = -1 / 0)
          }
          for (let e = w; e < g; e += 6) {
            let i = ~~((n[e + 2 * t] - r) / y)
            i >= f && (i = 31)
            const s = h[i]
            s.count++
            const o = s.bounds
            for (let t = 0; t < 3; t++) {
              const r = n[e + 2 * t],
                i = n[e + 2 * t + 1],
                s = r - i,
                a = r + i
              s < o[t] && (o[t] = s), a > o[t + 3] && (o[t + 3] = a)
            }
          }
          const x = h[31]
          o(x.bounds, x.rightCacheBounds)
          for (let t = 30; t >= 0; t--) {
            const e = h[t],
              n = h[t + 1]
            a(e.bounds, n.rightCacheBounds, e.rightCacheBounds)
          }
          let m = 0
          for (let e = 0; e < 31; e++) {
            const n = h[e],
              r = n.count,
              f = n.bounds,
              w = h[e + 1].rightCacheBounds
            0 !== r && (0 === m ? o(f, d) : a(f, d, d)), (m += r)
            let g = 0,
              y = 0
            0 !== m && (g = c(d) / s)
            const x = i - m
            0 !== x && (y = c(w) / s)
            const B = 1 + 1.25 * (g * m + y * x)
            B < u && ((l = t), (u = B), (p = n.candidate))
          }
        }
      }
      return { axis: l, pos: p }
    })(e.boundingData, u, w, r, i, B)
    if (-1 === T.axis) return (e.offset = r), (e.count = i), e
    const M = (function (t, e, n, r, i) {
      let s = n,
        o = n + r - 1
      const a = i.pos,
        c = 2 * i.axis
      for (;;) {
        for (; s <= o && e[6 * s + c] < a; ) s++
        for (; s <= o && e[6 * o + c] >= a; ) o--
        if (!(s < o)) return s
        for (let n = 0; n < 3; n++) {
          let r = t[3 * s + n]
          ;(t[3 * s + n] = t[3 * o + n]), (t[3 * o + n] = r)
          let i = e[6 * s + 2 * n + 0]
          ;(e[6 * s + 2 * n + 0] = e[6 * o + 2 * n + 0]), (e[6 * o + 2 * n + 0] = i)
          let a = e[6 * s + 2 * n + 1]
          ;(e[6 * s + 2 * n + 1] = e[6 * o + 2 * n + 1]), (e[6 * o + 2 * n + 1] = a)
        }
        s++, o--
      }
    })(g, w, r, i, T)
    if (M === r || M === r + i) (e.offset = r), (e.count = i)
    else {
      e.splitAxis = T.axis
      const t = new MeshBVHNode(),
        s = r,
        o = M - r
      ;(e.left = t), (t.boundingData = new Float32Array(6)), l(w, s, o, t.boundingData, p), n(t, s, o, p, V + 1)
      const a = new MeshBVHNode(),
        c = M,
        u = i - o
      ;(e.right = a), (a.boundingData = new Float32Array(6)), l(w, c, u, a.boundingData, p), n(a, c, u, p, V + 1)
    }
    return e
  }
  if (!t.index) {
    const n = t.attributes.position.count,
      i = e.useSharedArrayBuffer ? SharedArrayBuffer : ArrayBuffer
    let s
    ;(s = n > 65535 ? new Uint32Array(new i(4 * n)) : new Uint16Array(new i(2 * n))), t.setIndex(new THREE.BufferAttribute(s, 1))
    for (let t = 0; t < n; t++) s[t] = t
  }
  const i = new Float32Array(6),
    p = new Float32Array(6),
    w = (function (t, e) {
      const n = t.attributes.position,
        r = n.array,
        i = t.index.array,
        s = i.length / 3,
        o = new Float32Array(6 * s),
        a = n.offset || 0
      let c = 3
      n.isInterleavedBufferAttribute && (c = n.data.stride)
      for (let t = 0; t < s; t++) {
        const n = 3 * t,
          s = 6 * t,
          l = i[n + 0] * c + a,
          f = i[n + 1] * c + a,
          h = i[n + 2] * c + a
        for (let t = 0; t < 3; t++) {
          const n = r[l + t],
            i = r[f + t],
            a = r[h + t]
          let c = n
          i < c && (c = i), a < c && (c = a)
          let d = n
          i > d && (d = i), a > d && (d = a)
          const p = (d - c) / 2,
            w = 2 * t
          ;(o[s + w + 0] = c + p), (o[s + w + 1] = p + (Math.abs(c) + p) * u), c < e[t] && (e[t] = c), d > e[t + 3] && (e[t + 3] = d)
        }
      }
      return o
    })(t, i),
    g = t.index.array,
    y = e.maxDepth,
    x = e.verbose,
    m = e.maxLeafTris,
    B = e.strategy
  let b = !1
  const V = [],
    T = (function (t) {
      if (!t.groups || !t.groups.length) return [{ offset: 0, count: t.index.count / 3 }]
      const e = [],
        n = new Set()
      for (const e of t.groups) n.add(e.start), n.add(e.start + e.count)
      const r = Array.from(n.values()).sort((t: any, e: any) => t - e)
      for (let t = 0; t < r.length - 1; t++) {
        const n: any = r[t],
          i: any = r[t + 1]
        e.push({ offset: n / 3, count: (i - n) / 3 })
      }
      return e
    })(t)
  if (1 === T.length) {
    const t = T[0],
      e = new MeshBVHNode()
    ;(e.boundingData = i),
      (function (t, e, n, r) {
        let i = 1 / 0,
          s = 1 / 0,
          o = 1 / 0,
          a = -1 / 0,
          c = -1 / 0,
          u = -1 / 0
        for (let r = 6 * e, l = 6 * (e + n); r < l; r += 6) {
          const e = t[r + 0]
          e < i && (i = e), e > a && (a = e)
          const n = t[r + 2]
          n < s && (s = n), n > c && (c = n)
          const l = t[r + 4]
          l < o && (o = l), l > u && (u = l)
        }
        ;(r[0] = i), (r[1] = s), (r[2] = o), (r[3] = a), (r[4] = c), (r[5] = u)
      })(w, t.offset, t.count, p),
      n(e, t.offset, t.count, p),
      V.push(e)
  } else
    for (let t of T) {
      const e = new MeshBVHNode()
      ;(e.boundingData = new Float32Array(6)), l(w, t.offset, t.count, e.boundingData, p), n(e, t.offset, t.count, p), V.push(e)
    }
  return V
}
const w = 65535
class SeparatingAxisBounds {
  min: number
  max: number
  setFromBox: (e: any, n: any) => void
  constructor() {
    ;(this.min = 1 / 0), (this.max = -1 / 0)
  }
  setFromPointsField(t, e) {
    let n = 1 / 0,
      r = -1 / 0
    for (let i = 0, s = t.length; i < s; i++) {
      const s = t[i][e]
      ;(n = s < n ? s : n), (r = s > r ? s : r)
    }
    ;(this.min = n), (this.max = r)
  }
  setFromPoints(t, e) {
    let n = 1 / 0,
      r = -1 / 0
    for (let i = 0, s = e.length; i < s; i++) {
      const s = e[i],
        o = t.dot(s)
      ;(n = o < n ? o : n), (r = o > r ? o : r)
    }
    ;(this.min = n), (this.max = r)
  }
  isSeparated(t) {
    return this.min > t.max || t.min > this.max
  }
}
SeparatingAxisBounds.prototype.setFromBox = (function () {
  const t = new THREE.Vector3()
  return function (e, n) {
    const r = n.min,
      i = n.max
    let s = 1 / 0,
      o = -1 / 0
    for (let n = 0; n <= 1; n++)
      for (let a = 0; a <= 1; a++)
        for (let c = 0; c <= 1; c++) {
          ;(t.x = r.x * n + i.x * (1 - n)), (t.y = r.y * a + i.y * (1 - a)), (t.z = r.z * c + i.z * (1 - c))
          const u = e.dot(t)
          ;(s = Math.min(u, s)), (o = Math.max(u, o))
        }
    ;(this.min = s), (this.max = o)
  }
})()

const g = (function () {
  const t = new THREE.Vector3(),
    e = new THREE.Vector3(),
    n = new THREE.Vector3()
  return function (r, i, s) {
    const o = r.start,
      a = t,
      c = i.start,
      u = e
    n.subVectors(o, c), t.subVectors(r.end, i.start), e.subVectors(i.end, i.start)
    const l = n.dot(u),
      f = u.dot(a),
      h = u.dot(u),
      d = n.dot(a),
      p = a.dot(a) * h - f * f
    let w, g
    ;(w = 0 !== p ? (l * f - d * h) / p : 0), (g = (l + w * f) / h), (s.x = w), (s.y = g)
  }
})()
const y = (function () {
  const t = new THREE.Vector2(),
    e = new THREE.Vector3(),
    n = new THREE.Vector3()
  return function (r, i, s, o) {
    g(r, i, t)
    let a = t.x,
      c = t.y
    if (a >= 0 && a <= 1 && c >= 0 && c <= 1) return r.at(a, s), void i.at(c, o)
    if (a >= 0 && a <= 1) return c < 0 ? i.at(0, o) : i.at(1, o), void r.closestPointToPoint(o, !0, s)
    if (c >= 0 && c <= 1) return a < 0 ? r.at(0, s) : r.at(1, s), void i.closestPointToPoint(s, !0, o)
    {
      let t, u
      ;(t = a < 0 ? r.start : r.end), (u = c < 0 ? i.start : i.end)
      const l = e,
        f = n
      return (
        r.closestPointToPoint(u, !0, e),
        i.closestPointToPoint(t, !0, n),
        l.distanceToSquared(u) <= f.distanceToSquared(t) ? (s.copy(l), void o.copy(u)) : (s.copy(t), void o.copy(f))
      )
    }
  }
})()
const x = (function () {
  const t = new THREE.Vector3(),
    e = new THREE.Vector3(),
    n = new THREE.Plane(),
    i = new THREE.Line3()
  return function (r, s) {
    const { radius: o, center: a } = r,
      { a: c, b: u, c: l } = s
    ;(i.start = c), (i.end = u)
    if (i.closestPointToPoint(a, !0, t).distanceTo(a) <= o) return !0
    ;(i.start = c), (i.end = l)
    if (i.closestPointToPoint(a, !0, t).distanceTo(a) <= o) return !0
    ;(i.start = u), (i.end = l)
    if (i.closestPointToPoint(a, !0, t).distanceTo(a) <= o) return !0
    const f = s.getPlane(n)
    if (Math.abs(f.distanceToPoint(a)) <= o) {
      const t = f.projectPoint(a, e)
      if (s.containsPoint(t)) return !0
    }
    return !1
  }
})()
class SeparatingAxisTriangle extends THREE.Triangle {
  isSeparatingAxisTriangle: boolean
  satAxes: THREE.Vector3[]
  satBounds: SeparatingAxisBounds[]
  points: THREE.Vector3[]
  sphere: THREE.Sphere
  plane: THREE.Plane
  needsUpdate: boolean
  closestPointToSegment: (r: any, i?: any, s?: any) => number
  intersectsTriangle: (r: any, d?: any) => boolean
  distanceToPoint: (e: any) => any
  distanceToTriangle: (r: any, o?: any, a?: any) => number
  constructor(...t) {
    super(...t),
      (this.isSeparatingAxisTriangle = !0),
      (this.satAxes = new Array(4).fill(null).map(() => new THREE.Vector3())),
      (this.satBounds = new Array(4).fill(null).map(() => new SeparatingAxisBounds())),
      (this.points = [this.a, this.b, this.c]),
      (this.sphere = new THREE.Sphere()),
      (this.plane = new THREE.Plane()),
      (this.needsUpdate = !1)
  }
  intersectsSphere(t) {
    return x(t, this)
  }
  update() {
    const t = this.a,
      e = this.b,
      n = this.c,
      r = this.points,
      i = this.satAxes,
      s = this.satBounds,
      o = i[0],
      a = s[0]
    this.getNormal(o), a.setFromPoints(o, r)
    const c = i[1],
      u = s[1]
    c.subVectors(t, e), u.setFromPoints(c, r)
    const l = i[2],
      f = s[2]
    l.subVectors(e, n), f.setFromPoints(l, r)
    const h = i[3],
      d = s[3]
    h.subVectors(n, t), d.setFromPoints(h, r), this.sphere.setFromPoints(this.points), this.plane.setFromNormalAndCoplanarPoint(o, t), (this.needsUpdate = !1)
  }
}
SeparatingAxisTriangle.prototype.closestPointToSegment = (function () {
  const t = new THREE.Vector3(),
    e = new THREE.Vector3(),
    n = new THREE.Line3()
  return function (r, i = null, s = null) {
    const { start: o, end: a } = r,
      c = this.points
    let u,
      l = 1 / 0
    for (let o = 0; o < 3; o++) {
      const a = (o + 1) % 3
      n.start.copy(c[o]), n.end.copy(c[a]), y(n, r, t, e), (u = t.distanceToSquared(e)), u < l && ((l = u), i && i.copy(t), s && s.copy(e))
    }
    return (
      this.closestPointToPoint(o, t),
      (u = o.distanceToSquared(t)),
      u < l && ((l = u), i && i.copy(t), s && s.copy(o)),
      this.closestPointToPoint(a, t),
      (u = a.distanceToSquared(t)),
      u < l && ((l = u), i && i.copy(t), s && s.copy(a)),
      Math.sqrt(l)
    )
  }
})()
SeparatingAxisTriangle.prototype.intersectsTriangle = (function () {
  const t = new SeparatingAxisTriangle(),
    e = new Array(3),
    n = new Array(3),
    i = new SeparatingAxisBounds(),
    s = new SeparatingAxisBounds(),
    o = new THREE.Vector3(),
    a = new THREE.Vector3(),
    c = new THREE.Vector3(),
    u = new THREE.Vector3(),
    l = new THREE.Line3(),
    f = new THREE.Line3(),
    h = new THREE.Line3()
  return function (r, d = null) {
    this.needsUpdate && this.update(), r.isSeparatingAxisTriangle ? r.needsUpdate && r.update() : (t.copy(r), t.update(), (r = t))
    const p = this.satBounds,
      w = this.satAxes
    ;(n[0] = r.a), (n[1] = r.b), (n[2] = r.c)
    for (let t = 0; t < 4; t++) {
      const e = p[t],
        r = w[t]
      if ((i.setFromPoints(r, n), e.isSeparated(i))) return !1
    }
    const g = r.satBounds,
      y = r.satAxes
    ;(e[0] = this.a), (e[1] = this.b), (e[2] = this.c)
    for (let t = 0; t < 4; t++) {
      const n = g[t],
        r = y[t]
      if ((i.setFromPoints(r, e), n.isSeparated(i))) return !1
    }
    for (let t = 0; t < 4; t++) {
      const r = w[t]
      for (let t = 0; t < 4; t++) {
        const a = y[t]
        if ((o.crossVectors(r, a), i.setFromPoints(o, e), s.setFromPoints(o, n), i.isSeparated(s))) return !1
      }
    }
    if (d) {
      const t = this.plane,
        e = r.plane
      if (Math.abs(t.normal.dot(e.normal)) > 1 - 1e-10)
        console.warn("SeparatingAxisTriangle.intersectsTriangle: Triangles are coplanar which does not support an output edge. Setting edge to 0, 0, 0."),
          d.start.set(0, 0, 0),
          d.end.set(0, 0, 0)
      else {
        const n = this.points
        let i = !1
        for (let t = 0; t < 3; t++) {
          const r = n[t],
            s = n[(t + 1) % 3]
          if ((l.start.copy(r), l.end.copy(s), e.intersectLine(l, i ? f.start : f.end))) {
            if (i) break
            i = !0
          }
        }
        const s = r.points
        let o = !1
        for (let e = 0; e < 3; e++) {
          const n = s[e],
            r = s[(e + 1) % 3]
          if ((l.start.copy(n), l.end.copy(r), t.intersectLine(l, o ? h.start : h.end))) {
            if (o) break
            o = !0
          }
        }
        if ((f.delta(a), h.delta(c), a.dot(c) < 0)) {
          let t = h.start
          ;(h.start = h.end), (h.end = t)
        }
        u.subVectors(f.start, h.start),
          u.dot(a) > 0 ? d.start.copy(f.start) : d.start.copy(h.start),
          u.subVectors(f.end, h.end),
          u.dot(a) < 0 ? d.end.copy(f.end) : d.end.copy(h.end)
      }
    }
    return !0
  }
})()
SeparatingAxisTriangle.prototype.distanceToPoint = (function () {
  const t = new THREE.Vector3()
  return function (e) {
    return this.closestPointToPoint(e, t), e.distanceTo(t)
  }
})()
SeparatingAxisTriangle.prototype.distanceToTriangle = (function () {
  const t = new THREE.Vector3(),
    e = new THREE.Vector3(),
    n = ["a", "b", "c"],
    i = new THREE.Line3(),
    s = new THREE.Line3()
  return function (r, o = null, a = null) {
    const c = o || a ? i : null
    if (this.intersectsTriangle(r, c)) return (o || a) && (o && c.getCenter(o), a && c.getCenter(a)), 0
    let u = 1 / 0
    for (let e = 0; e < 3; e++) {
      let i
      const s = n[e],
        c = r[s]
      this.closestPointToPoint(c, t), (i = c.distanceToSquared(t)), i < u && ((u = i), o && o.copy(t), a && a.copy(c))
      const l = this[s]
      r.closestPointToPoint(l, t), (i = l.distanceToSquared(t)), i < u && ((u = i), o && o.copy(l), a && a.copy(t))
    }
    for (let c = 0; c < 3; c++) {
      const l = n[c],
        f = n[(c + 1) % 3]
      i.set(this[l], this[f])
      for (let c = 0; c < 3; c++) {
        const l = n[c],
          f = n[(c + 1) % 3]
        s.set(r[l], r[f]), y(i, s, t, e)
        const h = t.distanceToSquared(e)
        h < u && ((u = h), o && o.copy(t), a && a.copy(e))
      }
    }
    return Math.sqrt(u)
  }
})()
class OrientedBox extends THREE.Box3 {
  isOrientedBox: boolean
  matrix: THREE.Matrix4
  invMatrix: THREE.Matrix4
  points: THREE.Vector3[]
  satAxes: THREE.Vector3[]
  satBounds: SeparatingAxisBounds[]
  alignedSatBounds: SeparatingAxisBounds[]
  needsUpdate: boolean
  update: () => void
  closestPointToPoint: (t: any, e: any) => any
  distanceToBox: (r: any, o?: number, a?: any, c?: any) => number
  constructor(...t) {
    super(...t),
      (this.isOrientedBox = !0),
      (this.matrix = new THREE.Matrix4()),
      (this.invMatrix = new THREE.Matrix4()),
      (this.points = new Array(8).fill(null).map(() => new THREE.Vector3())),
      (this.satAxes = new Array(3).fill(null).map(() => new THREE.Vector3())),
      (this.satBounds = new Array(3).fill(null).map(() => new SeparatingAxisBounds())),
      (this.alignedSatBounds = new Array(3).fill(null).map(() => new SeparatingAxisBounds())),
      (this.needsUpdate = !1)
  }
  set(t: any, e: any, n?: any) {
    super.set(t, e), (this.matrix = n), (this.needsUpdate = !0)
    return this
  }
  copy(t) {
    super.copy(t), this.matrix.copy(t.matrix), (this.needsUpdate = !0)
    return this
  }
}
OrientedBox.prototype.update = function () {
  const t = this.matrix,
    e = this.min,
    n = this.max,
    r = this.points
  for (let i = 0; i <= 1; i++)
    for (let s = 0; s <= 1; s++)
      for (let o = 0; o <= 1; o++) {
        const a = r[(1 * i) | (2 * s) | (4 * o)]
        ;(a.x = i ? n.x : e.x), (a.y = s ? n.y : e.y), (a.z = o ? n.z : e.z), a.applyMatrix4(t)
      }
  const i = this.satBounds,
    s = this.satAxes,
    o = r[0]
  for (let t = 0; t < 3; t++) {
    const e = s[t],
      n = i[t],
      a = r[1 << t]
    e.subVectors(o, a), n.setFromPoints(e, r)
  }
  const a = this.alignedSatBounds
  a[0].setFromPointsField(r, "x"),
    a[1].setFromPointsField(r, "y"),
    a[2].setFromPointsField(r, "z"),
    this.invMatrix.copy(this.matrix).invert(),
    (this.needsUpdate = !1)
}
OrientedBox.prototype.intersectsBox = (function () {
  const t = new SeparatingAxisBounds()
  return function (e) {
    this.needsUpdate && this.update()
    const n = e.min,
      r = e.max,
      i = this.satBounds,
      s = this.satAxes,
      o = this.alignedSatBounds
    if (((t.min = n.x), (t.max = r.x), o[0].isSeparated(t))) return !1
    if (((t.min = n.y), (t.max = r.y), o[1].isSeparated(t))) return !1
    if (((t.min = n.z), (t.max = r.z), o[2].isSeparated(t))) return !1
    for (let n = 0; n < 3; n++) {
      const r = s[n],
        o = i[n]
      if ((t.setFromBox(r, e), o.isSeparated(t))) return !1
    }
    return !0
  }
})()
OrientedBox.prototype.intersectsTriangle = (function () {
  const t = new SeparatingAxisTriangle(),
    e = new Array(3),
    n = new SeparatingAxisBounds(),
    i = new SeparatingAxisBounds(),
    s = new THREE.Vector3()
  return function (r: any) {
    this.needsUpdate && this.update(), r.isSeparatingAxisTriangle ? r.needsUpdate && r.update() : (t.copy(r), t.update(), (r = t))
    const o = this.satBounds,
      a = this.satAxes
    ;(e[0] = r.a), (e[1] = r.b), (e[2] = r.c)
    for (let t = 0; t < 3; t++) {
      const r = o[t],
        i = a[t]
      if ((n.setFromPoints(i, e), r.isSeparated(n))) return !1
    }
    const c = r.satBounds,
      u = r.satAxes,
      l = this.points
    for (let t = 0; t < 3; t++) {
      const e = c[t],
        r = u[t]
      if ((n.setFromPoints(r, l), e.isSeparated(n))) return !1
    }
    for (let t = 0; t < 3; t++) {
      const r = a[t]
      for (let t = 0; t < 4; t++) {
        const o = u[t]
        if ((s.crossVectors(r, o), n.setFromPoints(s, e), i.setFromPoints(s, l), n.isSeparated(i))) return !1
      }
    }
    return !0
  }
})()
OrientedBox.prototype.closestPointToPoint = function (t, e) {
  return this.needsUpdate && this.update(), e.copy(t).applyMatrix4(this.invMatrix).clamp(this.min, this.max).applyMatrix4(this.matrix), e
}
OrientedBox.prototype.distanceToPoint = (function () {
  const t = new THREE.Vector3()
  return function (e) {
    return this.closestPointToPoint(e, t), e.distanceTo(t)
  }
})()
OrientedBox.prototype.distanceToBox = (function () {
  const t = ["x", "y", "z"],
    e = new Array(12).fill(null).map(() => new THREE.Line3()),
    n = new Array(12).fill(null).map(() => new THREE.Line3()),
    i = new THREE.Vector3(),
    s = new THREE.Vector3()
  return function (r, o = 0, a = null, c = null) {
    if ((this.needsUpdate && this.update(), this.intersectsBox(r)))
      return (a || c) && (r.getCenter(s), this.closestPointToPoint(s, i), r.closestPointToPoint(i, s), a && a.copy(i), c && c.copy(s)), 0
    const u = o * o,
      l = r.min,
      f = r.max,
      h = this.points
    let d = 1 / 0
    for (let t = 0; t < 8; t++) {
      const e = h[t]
      s.copy(e).clamp(l, f)
      const n = e.distanceToSquared(s)
      if (n < d && ((d = n), a && a.copy(e), c && c.copy(s), n < u)) return Math.sqrt(n)
    }
    let p = 0
    for (let r = 0; r < 3; r++)
      for (let i = 0; i <= 1; i++)
        for (let s = 0; s <= 1; s++) {
          const o = (r + 1) % 3,
            a = (r + 2) % 3,
            c = (1 << r) | (i << o) | (s << a),
            u = h[(i << o) | (s << a)],
            d = h[c]
          e[p].set(u, d)
          const w = t[r],
            g = t[o],
            y = t[a],
            x = n[p],
            m = x.start,
            B = x.end
          ;(m[w] = l[w]), (m[g] = i ? l[g] : f[g]), (m[y] = s ? l[y] : f[g]), (B[w] = f[w]), (B[g] = i ? l[g] : f[g]), (B[y] = s ? l[y] : f[g]), p++
        }
    for (let t = 0; t <= 1; t++)
      for (let e = 0; e <= 1; e++)
        for (let n = 0; n <= 1; n++) {
          ;(s.x = t ? f.x : l.x), (s.y = e ? f.y : l.y), (s.z = n ? f.z : l.z), this.closestPointToPoint(s, i)
          const r = s.distanceToSquared(i)
          if (r < d && ((d = r), a && a.copy(i), c && c.copy(s), r < u)) return Math.sqrt(r)
        }
    for (let t = 0; t < 12; t++) {
      const r = e[t]
      for (let t = 0; t < 12; t++) {
        const e = n[t]
        y(r, e, i, s)
        const o = i.distanceToSquared(s)
        if (o < d && ((d = o), a && a.copy(i), c && c.copy(s), o < u)) return Math.sqrt(o)
      }
    }
    return Math.sqrt(d)
  }
})()
import m from "../other/947-34003"
function B(t, e, n, r) {
  const i = t.a,
    s = t.b,
    o = t.c
  let a = e,
    c = e + 1,
    u = e + 2
  n && ((a = n.getX(e)), (c = n.getX(e + 1)), (u = n.getX(e + 2))),
    (i.x = r.getX(a)),
    (i.y = r.getY(a)),
    (i.z = r.getZ(a)),
    (s.x = r.getX(c)),
    (s.y = r.getY(c)),
    (s.z = r.getZ(c)),
    (o.x = r.getX(u)),
    (o.y = r.getY(u)),
    (o.z = r.getZ(u))
}
function b(t, e, n, r, i, s, o) {
  const a = n.index,
    c = n.attributes.position
  for (let n = t, u = e + t; n < u; n++) if ((B(o, 3 * n, a, c), (o.needsUpdate = !0), r(o, n, i, s))) return !0
  return !1
}
class PrimitivePool {
  _getNewPrimitive: any
  _primitives: any[]
  constructor(t) {
    ;(this._getNewPrimitive = t), (this._primitives = [])
  }
  getPrimitive() {
    const t = this._primitives
    return 0 === t.length ? this._getNewPrimitive() : t.pop()
  }
  releasePrimitive(t) {
    this._primitives.push(t)
  }
}
function V(t, e) {
  return 65535 === e[t + 15]
}
function T(t, e) {
  return e[t + 6]
}
function M(t, e) {
  return e[t + 14]
}
function P(t) {
  return t + 8
}
function S(t, e) {
  return e[t + 6]
}
const v = new THREE.Box3()
const F = new THREE.Vector3()
const A = ["x", "y", "z"]
function U(t, e, n, r, i) {
  let s = 2 * t,
    o = q,
    a = C,
    c = L
  if (V(s, a)) {
    const o = T(t, c),
      u = M(s, a)
    m.U$(e, n, r, o, u, i)
  } else {
    const s = P(t)
    O(s, o, r, F) && U(s, e, n, r, i)
    const a = S(t, c)
    O(a, o, r, F) && U(a, e, n, r, i)
  }
}
function H(t, e, n, r) {
  let i = 2 * t,
    s = q,
    o = C,
    a = L
  if (V(i, o)) {
    const s = T(t, a),
      c = M(i, o)
    return m.rM(e, n, r, s, c)
  }
  {
    const i = (function (t, e) {
        return e[t + 7]
      })(t, a),
      o = A[i],
      c = r.direction[o] >= 0
    let u, l
    c ? ((u = P(t)), (l = S(t, a))) : ((u = S(t, a)), (l = P(t)))
    const f = O(u, s, r, F) ? H(u, e, n, r) : null
    if (f) {
      const t = f.point[o]
      if (c ? t <= s[l + i] : t >= s[l + i + 3]) return f
    }
    const h = O(l, s, r, F) ? H(l, e, n, r) : null
    return f && h ? (f.distance <= h.distance ? f : h) : f || h || null
  }
}
const k = (function () {
  let t, e
  const n = [],
    s = new PrimitivePool(() => new THREE.Box3())
  return function (...r) {
    ;(t = s.getPrimitive()), (e = s.getPrimitive()), n.push(t, e)
    //@ts-ignore
    const i = o(...r)
    s.releasePrimitive(t), s.releasePrimitive(e), n.pop(), n.pop()
    const a = n.length
    return a > 0 && ((e = n[a - 1]), (t = n[a - 2])), i
  }
  function o(n, r, s, a, c = null, u = 0, l = 0) {
    function f(t) {
      let e = 2 * t,
        n = C,
        r = L
      for (; !V(e, n); ) e = 2 * (t = P(t))
      return T(t, r)
    }
    function h(t) {
      let e = 2 * t,
        n = C,
        r = L
      for (; !V(e, n); ) e = 2 * (t = S(t, r))
      return T(t, r) + M(e, n)
    }
    let d = 2 * n,
      p = q,
      w = C,
      g = L
    if (V(d, w)) {
      const e = T(n, g),
        r = M(d, w)
      return i(n, p, t), a(e, r, !1, l, u + n, t)
    }
    {
      const d = P(n),
        y = S(n, g)
      let x,
        m,
        B,
        b,
        T = d,
        M = y
      if (c && ((B = t), (b = e), i(T, p, B), i(M, p, b), (x = c(B)), (m = c(b)), m < x)) {
        ;(T = y), (M = d)
        const t = x
        ;(x = m), (m = t), (B = b)
      }
      B || ((B = t), i(T, p, B))
      const v = s(B, V(2 * T, w), x, l + 1, u + T)
      let F
      if (2 === v) {
        const t = f(T)
        F = a(t, h(T) - t, !0, l + 1, u + T, B)
      } else F = v && o(T, r, s, a, c, u, l + 1)
      if (F) return !0
      ;(b = e), i(M, p, b)
      const A = s(b, V(2 * M, w), m, l + 1, u + M)
      let U
      if (2 === A) {
        const t = f(M)
        U = a(t, h(M) - t, !0, l + 1, u + M, b)
      } else U = A && o(M, r, s, a, c, u, l + 1)
      return !!U
    }
  }
})()
const z = (function () {
  const t = new SeparatingAxisTriangle(),
    e = new SeparatingAxisTriangle(),
    n = new THREE.Matrix4(),
    s = new OrientedBox(),
    o = new OrientedBox()
  return function r(a, c, u, l, f = null) {
    let h = 2 * a,
      d = q,
      p = C,
      w = L
    null === f && (u.boundingBox || u.computeBoundingBox(), s.set(u.boundingBox.min, u.boundingBox.max, l), (f = s))
    if (!V(h, p)) {
      const t = a + 8,
        e = w[a + 6]
      i(t, d, v)
      if (f.intersectsBox(v) && r(t, c, u, l, f)) return !0
      i(e, d, v)
      return !!(f.intersectsBox(v) && r(e, c, u, l, f))
    }
    {
      const r = c,
        s = r.index,
        f = r.attributes.position,
        g = u.index,
        y = u.attributes.position,
        x = T(a, w),
        m = M(h, p)
      if ((n.copy(l).invert(), u.boundsTree)) {
        i(a, d, o), o.matrix.copy(n), (o.needsUpdate = !0)
        return u.boundsTree.shapecast({
          intersectsBounds: t => o.intersectsBox(t),
          intersectsTriangle: t => {
            t.a.applyMatrix4(l), t.b.applyMatrix4(l), t.c.applyMatrix4(l), (t.needsUpdate = !0)
            for (let n = 3 * x, r = 3 * (m + x); n < r; n += 3) if ((B(e, n, s, f), (e.needsUpdate = !0), t.intersectsTriangle(e))) return !0
            return !1
          }
        })
      }
      for (let r = 3 * x, i = m + 3 * x; r < i; r += 3) {
        B(t, r, s, f), t.a.applyMatrix4(n), t.b.applyMatrix4(n), t.c.applyMatrix4(n), (t.needsUpdate = !0)
        for (let n = 0, r = g.count; n < r; n += 3) if ((B(e, n, g, y), (e.needsUpdate = !0), t.intersectsTriangle(e))) return !0
      }
    }
  }
})()
function O(t, e, n, r) {
  return i(t, e, v), n.intersectBox(v, r)
}
const I = []
let D, q, C, L
function N(t) {
  D && I.push(D)
  D = t
  q = new Float32Array(t)
  C = new Uint16Array(t)
  L = new Uint32Array(t)
}
function _() {
  D = null
  q = null
  C = null
  L = null
  I.length && N(I.pop())
}
const E = Symbol("skip tree generation")
const G = new THREE.Box3()
const R = new THREE.Box3()
const X = new THREE.Matrix4()
const j = new OrientedBox()
const $ = new OrientedBox()
const Y = new THREE.Vector3()
const Z = new THREE.Vector3()
const W = new THREE.Vector3()
const J = new THREE.Vector3()
const K = new THREE.Vector3()
const Q = new THREE.Box3()
const tt = new PrimitivePool(() => new SeparatingAxisTriangle())
export default class MeshBVH {
  _roots: any
  geometry: any
  static serialize(t, e: any = {}) {
    if (e.isBufferGeometry)
      return (
        console.warn("MeshBVH.serialize: The arguments for the function have changed. See documentation for new signature."),
        MeshBVH.serialize(arguments[0], {
          cloneBuffers: void 0 === arguments[2] || arguments[2]
        })
      )
    e = { cloneBuffers: !0, ...e }
    const n = t.geometry,
      r = t._roots,
      i = n.getIndex()
    let s
    return (s = e.cloneBuffers ? { roots: r.map(t => t.slice()), index: i.array.slice() } : { roots: r, index: i.array }), s
  }
  static deserialize(t, e, n: any = {}) {
    if ("boolean" == typeof n)
      return (
        console.warn("MeshBVH.deserialize: The arguments for the function have changed. See documentation for new signature."),
        MeshBVH.deserialize(arguments[0], arguments[1], {
          setIndex: void 0 === arguments[2] || arguments[2]
        })
      )
    n = { setIndex: !0, ...n }
    const { index: i, roots: s } = t,
      o = new MeshBVH(e, { ...n, [E]: !0 })
    if (((o._roots = s), n.setIndex)) {
      const n = e.getIndex()
      if (null === n) {
        const n = new THREE.BufferAttribute(t.index, 1, !1)
        e.setIndex(n)
      } else n.array !== i && (n.array.set(i), (n.needsUpdate = !0))
    }
    return o
  }
  constructor(t: THREE.BufferGeometry, e: any = {}) {
    if (!t.isBufferGeometry) throw new Error("MeshBVH: Only BufferGeometries are supported.")
    if (t.index && t.index["isInterleavedBufferAttribute"]) throw new Error("MeshBVH: InterleavedBufferAttribute is not supported for the index attribute.")
    e = Object.assign(
      {
        strategy: 0,
        maxDepth: 40,
        maxLeafTris: 10,
        verbose: !0,
        useSharedArrayBuffer: !1,
        setBoundingBox: !0,
        [E]: !1
      },
      e
    )
    if (e.useSharedArrayBuffer && "undefined" == typeof SharedArrayBuffer) throw new Error("MeshBVH: SharedArrayBuffer is not available.")
    this._roots = null
    e[E] ||
      ((this._roots = ((t, e: any) => {
        const n = p(t, e)
        let r, i, s
        const o = [],
          a = e.useSharedArrayBuffer ? SharedArrayBuffer : ArrayBuffer
        for (let t = 0; t < n.length; t++) {
          const e = n[t]
          const l = new a(32 * c(e))
          r = new Float32Array(l)
          i = new Uint32Array(l)
          s = new Uint16Array(l)
          u(0, e)
          o.push(l)
        }
        return o
        function c(t) {
          return t.count ? 1 : 1 + c(t.left) + c(t.right)
        }
        function u(t, e) {
          const n = t / 4,
            o = t / 2,
            a = !!e.count,
            c = e.boundingData
          for (let t = 0; t < 6; t++) r[n + t] = c[t]
          if (a) {
            const r = e.offset,
              a = e.count
            return (i[n + 6] = r), (s[o + 14] = a), (s[o + 15] = w), t + 32
          }
          {
            const r = e.left,
              s = e.right,
              o = e.splitAxis
            let a
            if (((a = u(t + 32, r)), a / 4 > Math.pow(2, 32))) throw new Error("MeshBVH: Cannot store child pointer greater than 32 bits.")
            return (i[n + 6] = a / 4), (a = u(a, s)), (i[n + 7] = o), a
          }
        }
      })(t, e)),
      !t.boundingBox && e.setBoundingBox && (t.boundingBox = this.getBoundingBox(new THREE.Box3())))
    this.geometry = t
  }
  refit(t = null) {
    t && Array.isArray(t) && (t = new Set(t))
    const e = this.geometry,
      n = e.index.array,
      r = e.attributes.position,
      i = r.array,
      s = r.offset || 0
    let o,
      a,
      c,
      u,
      l = 3
    r.isInterleavedBufferAttribute && (l = r.data.stride)
    let f = 0
    const h = this._roots
    for (let t = 0, e = h.length; t < e; t++)
      (o = h[t]), (a = new Uint32Array(o)), (c = new Uint16Array(o)), (u = new Float32Array(o)), d(0, f), (f += o.byteLength)
    function d(e, r, o = !1) {
      const f = 2 * e
      if (c[f + 15] === w) {
        const t = a[e + 6]
        let r = 1 / 0,
          o = 1 / 0,
          h = 1 / 0,
          d = -1 / 0,
          p = -1 / 0,
          w = -1 / 0
        for (let e = 3 * t, a = 3 * (t + c[f + 14]); e < a; e++) {
          const t = n[e] * l + s,
            a = i[t + 0],
            c = i[t + 1],
            u = i[t + 2]
          a < r && (r = a), a > d && (d = a), c < o && (o = c), c > p && (p = c), u < h && (h = u), u > w && (w = u)
        }
        return (
          (u[e + 0] !== r || u[e + 1] !== o || u[e + 2] !== h || u[e + 3] !== d || u[e + 4] !== p || u[e + 5] !== w) &&
          ((u[e + 0] = r), (u[e + 1] = o), (u[e + 2] = h), (u[e + 3] = d), (u[e + 4] = p), (u[e + 5] = w), !0)
        )
      }
      {
        const n = e + 8,
          i = a[e + 6],
          s = n + r,
          c = i + r
        let l = o,
          f = !1,
          h = !1
        t ? l || ((f = t.has(s)), (h = t.has(c)), (l = !f && !h)) : ((f = !0), (h = !0))
        const p = l || h
        let w = !1
        ;(l || f) && (w = d(n, r, l))
        let g = !1
        p && (g = d(i, r, l))
        const y = w || g
        if (y)
          for (let t = 0; t < 3; t++) {
            const r = n + t,
              s = i + t,
              o = u[r],
              a = u[r + 3],
              c = u[s],
              l = u[s + 3]
            ;(u[e + t] = o < c ? o : c), (u[e + t + 3] = a > l ? a : l)
          }
        return y
      }
    }
  }
  traverse(t, e = 0) {
    const n = this._roots[e],
      r = new Uint32Array(n),
      i = new Uint16Array(n)
    function e1(s, o = 0) {
      const a = 2 * s,
        c = i[a + 15] === w
      if (c) {
        const e = r[s + 6],
          u = i[a + 14]
        t(o, c, new Float32Array(n, 4 * s, 6), e, u)
      } else {
        const i = s + 8,
          a = r[s + 6],
          u = r[s + 7]
        t(o, c, new Float32Array(n, 4 * s, 6), u) || (e1(i, o + 1), e1(a, o + 1))
      }
    }
    e1(0)
  }
  raycast(t, e: any = THREE.FrontSide) {
    const n = this._roots,
      i = this.geometry,
      s = [],
      o = e.isMaterial,
      a = Array.isArray(e),
      c = i.groups,
      u = o ? e.side : e
    for (let r = 0, o = n.length; r < o; r++) {
      const o = a ? e[c[r].materialIndex].side : u,
        l = s.length
      if ((N(n[r]), U(0, i, o, t, s), _(), a)) {
        const t = c[r].materialIndex
        for (let e = l, n = s.length; e < n; e++) s[e].face.materialIndex = t
      }
    }
    return s
  }
  raycastFirst(t: THREE.Ray, e: THREE.Material) {
    const n = this._roots
    const i = this.geometry
    const s = e.isMaterial
    const o = Array.isArray(e)
    let a = null
    const c = i.groups,
      u = s ? e.side : e
    for (let r = 0, s = n.length; r < s; r++) {
      const s = o ? e[c[r].materialIndex].side : u
      N(n[r])
      const l = H(0, i, s, t)
      _(), null != l && (null == a || l.distance < a.distance) && ((a = l), o && (l.face.materialIndex = c[r].materialIndex))
    }
    return a
  }
  intersectsGeometry(t, e) {
    const n = this.geometry
    let r = !1
    for (const i of this._roots) if ((N(i), (r = z(0, n, t, e)), _(), r)) break
    return r
  }
  shapecast(t, e?, n?) {
    const r = this.geometry
    if (t instanceof Function) {
      if (e) {
        const t = e
        e = (e, n, r, i) => {
          const s = 3 * n
          return t(e, s, s + 1, s + 2, r, i)
        }
      }
      ;(t = {
        boundsTraverseOrder: n,
        intersectsBounds: t,
        intersectsTriangle: e,
        intersectsRange: null
      }),
        console.warn("MeshBVH: Shapecast function signature has changed and now takes an object of callbacks as a second argument. See docs for new signature.")
    }
    const i = tt.getPrimitive()
    let { boundsTraverseOrder: s, intersectsBounds: o, intersectsRange: a, intersectsTriangle: c } = t
    if (a && c) {
      const t = a
      a = (e, n, s, o, a) => !!t(e, n, s, o, a) || b(e, n, r, c, s, o, i)
    } else a || (a = c ? (t, e, n, s) => b(t, e, r, c, n, s, i) : (t, e, n) => n)
    let u = !1,
      l = 0
    for (const t of this._roots) {
      if ((N(t), (u = k(0, r, o, a, s, l)), _(), u)) break
      l += t.byteLength
    }
    return tt.releasePrimitive(i), u
  }
  bvhcast(t, e, n) {
    let { intersectsRanges: r, intersectsTriangles: i } = n
    const s = t.geometry,
      o = s.index,
      a = s.attributes.position
    X.copy(e).invert()
    const c = tt.getPrimitive(),
      u = tt.getPrimitive()
    function l(t, n, r, s, l, f, h, d) {
      for (let p = r, w = r + s; p < w; p++) {
        B(u, 3 * p, o, a), u.a.applyMatrix4(e), u.b.applyMatrix4(e), u.c.applyMatrix4(e), (u.needsUpdate = !0)
        for (let e = t, r = t + n; e < r; e++) if ((B(c, 3 * e, o, a), (c.needsUpdate = !0), i(c, u, e, p, l, f, h, d))) return !0
      }
      return !1
    }
    if (i) {
      if (r) {
        const t = r
        r = function (e, n, r, i, s, o, a, c) {
          return !!t(e, n, r, i, s, o, a, c) || l(e, n, r, i, s, o, a, c)
        }
      } else r = l
    }
    this.getBoundingBox(R), R.applyMatrix4(e)
    const f = this.shapecast({
      intersectsBounds: t => R.intersectsBox(t),
      intersectsRange: (e, n, i, s, o, a) => (
        G.copy(a),
        G.applyMatrix4(X),
        t.shapecast({
          intersectsBounds: t => G.intersectsBox(t),
          intersectsRange: (t, i, a, c, u) => r(e, n, t, i, s, o, c, u)
        })
      )
    })
    return tt.releasePrimitive(c), tt.releasePrimitive(u), f
  }
  intersectsBox(t, e) {
    return (
      j.set(t.min, t.max, e),
      (j.needsUpdate = !0),
      this.shapecast({
        intersectsBounds: t => j.intersectsBox(t),
        intersectsTriangle: t => j.intersectsTriangle(t)
      })
    )
  }
  intersectsSphere(t) {
    return this.shapecast({
      intersectsBounds: e => t.intersectsBox(e),
      intersectsTriangle: e => e.intersectsSphere(t)
    })
  }
  closestPointToGeometry(t, e, n: any = {}, r: any = {}, i = 0, s = 1 / 0) {
    t.boundingBox || t.computeBoundingBox(), j.set(t.boundingBox.min, t.boundingBox.max, e), (j.needsUpdate = !0)
    const o = this.geometry,
      a = o.attributes.position,
      c = o.index,
      u = t.attributes.position,
      l = t.index,
      f = tt.getPrimitive(),
      h = tt.getPrimitive()
    let d = Z,
      p = W,
      w = null,
      g = null
    r && ((w = J), (g = K))
    let y = 1 / 0,
      x = null,
      m = null
    return (
      X.copy(e).invert(),
      $.matrix.copy(X),
      this.shapecast({
        boundsTraverseOrder: t => j.distanceToBox(t, Math.min(y, s)),
        intersectsBounds: (t, e, n) => n < y && n < s && (e && ($.min.copy(t.min), $.max.copy(t.max), ($.needsUpdate = !0)), !0),
        intersectsRange: (n, r) => {
          if (t.boundsTree)
            return t.boundsTree.shapecast({
              boundsTraverseOrder: t => $.distanceToBox(t, Math.min(y, s)),
              intersectsBounds: (t, e, n) => n < y && n < s,
              intersectsRange: (t, s) => {
                for (let o = 3 * t, b = 3 * (t + s); o < b; o += 3) {
                  B(h, o, l, u), h.a.applyMatrix4(e), h.b.applyMatrix4(e), h.c.applyMatrix4(e), (h.needsUpdate = !0)
                  for (let t = 3 * n, e = 3 * (n + r); t < e; t += 3) {
                    B(f, t, c, a), (f.needsUpdate = !0)
                    const e = f.distanceToTriangle(h, d, w)
                    if ((e < y && (p.copy(d), g && g.copy(w), (y = e), (x = t / 3), (m = o / 3)), e < i)) return !0
                  }
                }
                return false
              }
            })
          for (let t = 0, s = l ? l.count : u.count; t < s; t += 3) {
            B(h, t, l, u), h.a.applyMatrix4(e), h.b.applyMatrix4(e), h.c.applyMatrix4(e), (h.needsUpdate = !0)
            for (let e = 3 * n, s = 3 * (n + r); e < s; e += 3) {
              B(f, e, c, a), (f.needsUpdate = !0)
              const n = f.distanceToTriangle(h, d, w)
              if ((n < y && (p.copy(d), g && g.copy(w), (y = n), (x = e / 3), (m = t / 3)), n < i)) return !0
            }
          }
        }
      }),
      tt.releasePrimitive(f),
      tt.releasePrimitive(h),
      y === 1 / 0
        ? null
        : (n.point ? n.point.copy(p) : (n.point = p.clone()),
          (n.distance = y),
          (n.faceIndex = x),
          r &&
            (r.point ? r.point.copy(g) : (r.point = g.clone()),
            r.point.applyMatrix4(X),
            p.applyMatrix4(X),
            (r.distance = p.sub(r.point).length()),
            (r.faceIndex = m)),
          n)
    )
  }
  closestPointToPoint(t: THREE.Vector3, e: any = {}, n = 0, r = 1 / 0) {
    const i = n * n
    const s = r * r
    let o = 1 / 0
    let a = null
    this.shapecast({
      boundsTraverseOrder: e => (Y.copy(t).clamp(e.min, e.max), Y.distanceToSquared(t)),
      intersectsBounds: (t, e, n) => n < o && n < s,
      intersectsTriangle: (e, n) => {
        e.closestPointToPoint(t, Y)
        const r = t.distanceToSquared(Y)
        return r < o && (Z.copy(Y), (o = r), (a = n)), r < i
      }
    })
    if (o === 1 / 0) return null
    const c = Math.sqrt(o)
    e.point ? e.point.copy(Z) : (e.point = Z.clone())
    e.distance = c
    e.faceIndex = a
    return e
  }
  getBoundingBox(t) {
    t.makeEmpty()
    return (
      this._roots.forEach(e => {
        i(0, new Float32Array(e), Q), t.union(Q)
      }),
      t
    )
  }
}
const et = MeshBVH.prototype.raycast
MeshBVH.prototype.raycast = function (...t: any) {
  if (t[0].isMesh) {
    console.warn('MeshBVH: The function signature and results frame for "raycast" has changed. See docs for new signature.')
    const [e, n, r, i] = t
    return (
      et.call(this, r, e.material).forEach(t => {
        ;(t = m.O(t, e, n)) && (i as any[]).push(t)
      }),
      i
    )
  }
  return et.apply(this, t)
}
const nt = MeshBVH.prototype.raycastFirst
MeshBVH.prototype.raycastFirst = function (...t: any) {
  if (t[0].isMesh) {
    console.warn('MeshBVH: The function signature and results frame for "raycastFirst" has changed. See docs for new signature.')
    const [e, n, r] = t
    return m.O(nt.call(this, r, e.material), e, n)
  }
  return nt.apply(this, t)
}
const rt = MeshBVH.prototype.closestPointToPoint
MeshBVH.prototype.closestPointToPoint = function (...t: any) {
  if (t[0].isMesh) {
    console.warn('MeshBVH: The function signature and results frame for "closestPointToPoint" has changed. See docs for new signature.'), t.unshift()
    const e = t[1],
      n: any = {}
    return (t[1] = n), rt.apply(this, t), e && e.copy(n.point), n.distance
  }
  return rt.apply(this, t)
}
const it = MeshBVH.prototype.closestPointToGeometry
MeshBVH.prototype.closestPointToGeometry = function (...t: any) {
  const e = t[2],
    n = t[3]
  if ((e && e.isVector3) || (n && n.isVector3)) {
    console.warn('MeshBVH: The function signature and results frame for "closestPointToGeometry" has changed. See docs for new signature.')
    const r: any = {},
      i: any = {},
      s = t[1]
    return (t[2] = r), (t[3] = i), it.apply(this, t), e && e.copy(r.point), n && n.copy(i.point).applyMatrix4(s), r.distance
  }
  return it.apply(this, t)
}
const st = MeshBVH.prototype.refit
MeshBVH.prototype.refit = function (...t: any) {
  const e = t[0],
    n = t[1]
  if (n && (n instanceof Set || Array.isArray(n))) {
    console.warn('MeshBVH: The function signature for "refit" has changed. See docs for new signature.')
    const t = new Set()
    n.forEach(e => t.add(e)), e && e.forEach(e => t.add(e)), st.call(this, t)
  } else st.apply(this, t)
}
;["intersectsGeometry", "shapecast", "intersectsBox", "intersectsSphere"].forEach(t => {
  const e = MeshBVH.prototype[t]
  MeshBVH.prototype[t] = function (...n) {
    return (
      (null === n[0] || n[0].isMesh) &&
        (n.shift(), console.warn(`MeshBVH: The function signature for "${t}" has changed and no longer takes Mesh. See docs for new signature.`)),
      e.apply(this, n)
    )
  }
})
